package com.scalar.chiptrack.operations.dataentry.rmaDisposition.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.dataentry.rmaDisposition.forms.RMADispositionForm;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.db.DBTransaction;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * User: ChandraSekhar
 * Date: Aug 29, 2008
 * Time: 5:22:54 PM
 */
public class RMADispositionSaveCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/rmaDisposition.do";

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {

        RMADispositionForm rmaDispositionForm = (RMADispositionForm) form;

        HashMap errors = new HashMap();
        HashMap info = new HashMap();
        String action = request.getParameter("action");

        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);
        DBTransaction dbTranasaction    = null;
        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }

        try
        {
            DataEntryDAO.saveRMAReceiveWIPInfo( dbTranasaction, rmaDispositionForm.getRmaLotsList(), user.getUserName() );
            resetToken( request );
            info.put("RMA Lots received to WIP Successlly", null);

            request.setAttribute("action", action);
       }
       catch( Exception e)
       {
          errors.put( "Could not save RMA WIP Lot details", null);
          e.printStackTrace();
       }
       finally
        {
            rmaDispositionForm.setProcessStepId(-1);
            rmaDispositionForm.setLocationId(-1);
            rmaDispositionForm.setRmaReceiveLotId(-1);
            rmaDispositionForm.setRmaLotsList( null);

            request.setAttribute("rmaLotsList", DataEntryDAO.getAllRMAReceivedLots( 0, rmaDispositionForm.getProcessStepId(), rmaDispositionForm.getLocationId(), rmaDispositionForm.getRmaReceiveLotId() ) );
            request.setAttribute("locationList", VendorDAO.getInventoryLocationsByProcessByUserType( null, LOCATION_TYPE_FINISHED_GOODS, user.getVendorId() ) );
            request.setAttribute("partNumbersList", ProductDao.loadAllPartsForProcessTypeByPsType( LOCATION_TYPE_FG ) );
        }
        return mapping.findForward("rmaDisposition");
    }
}
